#include "Precomp.h"
#include "AllBindings.h"
#include "InputManager.h"
#include "SQGame.h"

int mapInput(HSQUIRRELVM vm)
{
	InputManager& inputManager=SQGame::getInstance().getInputManager();
	StackHandler s(vm);
	if(s.GetParamCount()<2)
		return sq_throwerror(vm,L"Not enough argument");
	const SQChar* input=s.GetString(2);
	SquirrelObject hit=s.GetObjectHandle(3);
	SquirrelObject release;
	if(s.GetParamCount()>2)//optional parameter
		release=s.GetObjectHandle(4);
	//type check
	if((release.GetType()!=OT_NATIVECLOSURE)
		&&(release.GetType()!=OT_CLOSURE)
		&&(release.GetType()!=OT_NULL)
		&&(hit.GetType()!=OT_NATIVECLOSURE)
		&&(hit.GetType()!=OT_CLOSURE)
		&&(hit.GetType()!=OT_NULL))
		return sq_throwerror(vm,L"Wrong type of argument");
	inputManager.mapInput(input,hit,release);
	return 0;//success
}

void bindInput()
{
	InputManager& inputManager=SQGame::getInstance().getInputManager();
	SquirrelVM::CreateFunctionGlobal(&mapInput,L"mapInput",L"*");
	RegisterGlobal(inputManager,&InputManager::isKeyDown,L"isKeyDown");
	RegisterGlobal(inputManager,&InputManager::isJoyDown,L"isJoyDown");
	RegisterGlobal(inputManager,&InputManager::isMouseDown,L"isMouseDown");
	RegisterGlobal(inputManager,&InputManager::mouseX,L"mouseX");
	RegisterGlobal(inputManager,&InputManager::mouseY,L"mouseY");
}